function [y,frac_sold_state,sale_price_state,th,et] = ds_urn_matching_one_price_static(params,State,psi,kh,ke,ka,graph)
lamb_1(2) = params(1); % this was missing was 0.1
lamb_1(3) = params(2);
lamb_2 = params(3);
lamb_1(1) = 1;
c_b = params(4);

lamb_1 = lamb_1(State(4,:));
gamm = 0.5;

M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'bust'); 
v(1) = M_data(14,1)/gamm;
M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'medium'); 
v(2) = M_data(14,1)/gamm;
M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'boom'); 
v(3) = M_data(14,1)/gamm;
    
lamb_vec_fun = @(e)lamb_1.*e.^lamb_2;
lamb_vec = lamb_vec_fun(State(1,:));
eta      = @(th)(1./th).*(1 - exp(-lamb_vec.*th)); % buyer match rate
mu       = @(th)1 - exp(-lamb_vec.*th); % seller match rate
v        = v(State(4,:));

%% Solution with one shot search with no price setting
numw = 1; % the possibility of multiple periods within one. 
sale_price_state(numw,:) = gamm*v;
et = c_b./(v-sale_price_state(numw,:));
fun = @(th)1./th.*(1-exp(-lamb_vec.*th))-et; % function
x0 = 1*ones(size(et)); % initial interval
options = optimset('Display','off');
th(numw,:) = fsolve(fun,x0,options);
Vs(numw,:) = mu(th(numw,:)).*sale_price_state(numw,:)*psi;

%% Statistics
frac_sold_state = mu(th); % sale probability

close all;

moments_model = [];
moments_data = [];
weights = [];

%% Matching to data with continuous bins:
for j =  1:kh 
    % Load data
    if j==1
        M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'bust'); 
    elseif j==2
        M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'medium'); 
    elseif j==3
        M_data = xlsread('data_for_model/save_calibrationcontall_experience_l1', 'boom'); 
    end
    close_prices_data = M_data(5,1:1:end);
    sale_prob_data = M_data(4,1:1:end);
    
    for i = 1:ke
        sale_prices_model(i) = max(log(sale_price_state).*(State(1,:)==i).*(State(4,:)==j));
        sale_prob_model(i) = max(frac_sold_state.*(State(1,:)==i).*(State(4,:)==j));
    end
    
    moments_model = [moments_model sale_prob_model]; %  list_prices_model  relative_dom_model sale_prices_model];
    moments_data = [moments_data sale_prob_data(1:i)]; %  list_prices_data  relative_dom_data close_prices_data];
    weights = [weights ones(size(sale_prob_model))]; %ones(size(moments_data));
if graph==1
  t = size(sale_prob_model,2);
  figure;
  subplot(1,2,1);
  plot(1:t,sale_prob_model(1:t),'r');
  hold on;
  plot(1:t,sale_prob_data(1:t),'b');
  title('Sale Probability');

  subplot(1,2,2);
  plot(1:t,exp(sale_prices_model(1:t)),'r');
  hold on;
  plot(1:t,exp(close_prices_data(1:t)),'b');
  title('Sale Price');
end

end

y = sum(((moments_data - moments_model)./moments_data).^2.*weights);
if min(moments_model)<0 | max(~isreal(moments_model)) | isinf(y) | isnan(y) | min(th)<0
    y = 10000*1/lamb_1(1); %*1/(params(end-1)+params(end-2)+params(end));
end

%% Save Results
load obj_min_search_one_price_static
if y<obj_min
    obj_min = y;
    params_min = params;
   % [params_min(1) params_min(2) params_min(3)]
    moments_data_min = moments_data;
    moments_model_min = moments_model;
    save obj_min_search_one_price_static.mat obj_min params_min moments_model_min moments_data_min;
    save obj_min_search_one_price_static_best.mat obj_min params_min moments_model_min moments_data_min;
end;


